package com.entor.hqj.main;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.entor.hqj.login.LoginActivity;
import com.entor.hqj.login.R;
import com.entor.hqj.login.RegisterActivity;
import com.entor.hqj.model.Json;
import com.entor.hqj.util.HttpPostUtils;
import com.entor.hqj.util.MoreTextView;
import com.entor.hqj.util.TitleBuilder;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

import java.net.URLEncoder;

public class ItemInfoEditActivity extends AppCompatActivity {
    private EditText et_title;
    private EditText et_content;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_item_info_edit);

        et_title = (EditText) findViewById(R.id.et_title);
        et_content = (EditText) findViewById(R.id.et_content);

        initTitle();
        initData();


        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    private void initTitle() {
        new TitleBuilder(this).setLeftText("返回").setMidTitleText("详细信息")
                .setRightText("编辑")
                .setLeftImgOrTextListener(new leftImgOrTextListener())
                .setRightImgOrTextListener(new rightImgOrTextListener());
    }


    class leftImgOrTextListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            finish();
        }
    }

    class rightImgOrTextListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            try {
                Intent intent = getIntent();
                Long id = intent.getLongExtra("id", 0);
                String name = et_title.getText().toString().trim();
                String content = et_content.getText().toString().trim();

                final String data = "name=" + name + "&content=" + content + "&id=" + id;
                new Thread() {
                    @Override
                    public void run() {
                        String response =
                                HttpPostUtils.doPostRequest("user/editByMobile", data);
                        Message msg = Message.obtain();
                        msg.obj = response;
                        handler.sendMessage(msg);
                    }
                }.start();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message message){
            String result = (String)message.obj;
            JSONObject resultJson =
                    JSON.parseObject(result);
            if(resultJson!=null) {
                String msg = resultJson.getString("msg");
                boolean sus = resultJson.getBoolean("success");
                Toast.makeText(ItemInfoEditActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        }
    };


    public void initData() {
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        String content = intent.getStringExtra("content");

        et_title.setText(name);
        et_content.setText(content);
    }
}
